using System;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Navigant.Infrastructure.Helpers;

namespace Navigant.Web.UI
{
	public abstract class CustodianWebPart : System.Web.UI.WebControls.WebParts.WebPart
	{
        protected SPWeb CurrentWeb
        {
            get { return SPControl.GetContextWeb(HttpContext.Current); }
        }

		protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            try
            {
                SPWeb currentWeb = SPControl.GetContextWeb(this.Context);
                Type currentType = this.GetType();
                string classResourcePath = SPWebPartManager.GetClassResourcePath(currentWeb, currentType);
                string scriptFileName = "Custodian.js";
                if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(scriptFileName))
                {
                    this.Page.ClientScript.RegisterClientScriptInclude(scriptFileName,
                        string.Format("{0}/{1}", classResourcePath, scriptFileName));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                this.WriteErrorToPage(ex);
            }
        }

		protected void WriteErrorToPage(Exception e)
		{
			#if DEBUG
				this.Page.Response.Write(e.Message + "<br>" + e.StackTrace);
			#endif
		}
	}
}
